/**
 * Created by JetBrains WebStorm.
 * User: mumble
 * Date: 3/11/12
 * Time: 12:18 AM
 * To change this template use File | Settings | File Templates.
 */
// Trang thai tro choi
var GAME_ROUND_WAITING = 1, // Trang thai choi nguoi choi
    GAME_ROUND_READY = 2,   // Trang thai san sang
    GAME_ROUND_RUNNING = 3, // Trang thai dang choi
    GAME_ROUND_FINISHED = 4; // Ket thuc

var GAME_PACKAGE = 1,
    PING_PACKAGE =2,
    CONTROL_PACKAGE = 3;


var GAME_CMD_PLAYER_JOIN = 1,
    GAME_CMD_PLAYER_SPAWN = 2,
    GAME_CMD_PLAYER_DIE = 3,
    GAME_CMD_PLAYER_FIRE = 4,
    GAME_CMD_PLAYER_OUT = 5,
    GAME_CMD_PLAYER_STATE = 6,
    GAME_CMD_PLAYER_CHAT = 7;

var CTRL_CMD_CLIENT_CONNECT = 1,
    CTRL_CMD_CLIENT_DISCONNECT = 2,
    CTRL_CMD_CLIENT_LOGIN = 3,
    CTRL_CMD_CLIENT_JOIN_ROOM = 4,
    CTRL_CMD_START_GAME = 5,
    CTRL_CMD_CLIENT_INFO = 6,
    CTRL_CMD_CREATE_ROOM = 7,
    CTRL_CMD_CLIENT_CHAT = 8,
    CTRL_CMD_SERVER_INFO = 9,
    CTRL_CMD_GAME_DATA = 10;




function World()
{
    this.mapRoom = {};
    this.mapClient = {};
    this.numOfClients = 0;
}

World.prototype = {
    validDateRoomName : function(roomName)
    {
        if(this.mapRoom[roomName] == undefined)
            return true;
        return false;
    },
    addRoom : function(roomName, owner)
    {
        var result = this.validDateRoomName(roomName);
        if(result == false)
            return false;

        var room = new GameRoom(roomName);
        this.mapRoom[roomName] = room;
        room.clients.push(owner);
        owner.setRoom(room);
    },
    addClient : function(connection)
    {
        var isFound = false;
       for(var id in this.mapClient)
       {
           if(this.mapClient[id].isConnected == false)
           {
               this.mapClient[id].setConnection(connection);
               isFound = true;
               break;
           }
       }
        if (!isFound)
        {
            var client = new Client(connection);
            this.numOfClients++;
            this.mapClient[this.numOfClients] = client;
            client.setClientId(this.numOfClients);
        }
    },
    joinRoom : function(roomName, client)
    {
        var room = this.mapRoom[roomName];
        if(room == undefined)
            return false;
        room.clients.push(client);
        room.broadcast([CONTROL_PACKAGE,[CTRL_CMD_CLIENT_JOIN_ROOM], client.name, client.highscore ]);
    },
    startRoom : function(owner)
    {
        var room = owner.room;
        if(room == undefined)
            return false;
        var length = room.clients;
        var jsonPlayerList = [];
        for(var i = 0; i < length; ++i)
        {
            var client = room.clients[i];
            var player = new Arena.Player(0,0,0);
            client.player = player;
            var jsonStatus = {'layername' : client.name, 'pos' : 0, 'velocity' : 0, 'heading' : 0};
            jsonPlayerList.push(jsonStatus);

        }
        var package = [GAME_PACKAGE, [CTRL_CMD_START_GAME, jsonPlayerList] ];
//      [GAME_PACKAGE, [START, [{playername, pos, velocity, heading}, {playername, pos, velocity, heading}]   ]]
    }

}


function Client(conn, world){
    this.client_id = -1;
    this.name = '';
    this.password = '';
    this.highscore = 0;
    this.lastscore = 0;
    this.player = null;
    this.conn = conn;
    this.isConnected = true;
    this.room = null;
    this.world = world;

}

Client.prototype = {
    login : function(name, password)
    {
        if(name != undefined && password != undefined)
        {
            this.name = name;
            this.password = password;
            return true;
        }
        return false;
    },
    setInformation : function(highCore, lasCore)
    {
        this.highscore = highCore;
        this.lastscore = lasCore;
    },
    setConnection : function(conn) {
        this.conn = conn;
        this.isConnected = true;
    },
    setDisconnect : function() {
        this.conn.close();
        delete this.conn;
        this.conn = null;
    },
    setClientId : function(id)
    {
        this.client_id = id;
    },
    setRoom : function(room)
    {
        this.room = room;
    }



}

function GameRoom(room_name)
{
    this.room_name = room_name;
    this.round_state = GAME_ROUND_WAITING;
    this.number_player  = 0;
    this.max_player = 5;
    this.map_enemy = {};
    //this.map_player = {};
    this.clients = [];
    this.enemys = [];
    this.player_spawn_points = [];
    this.enemys_spawn_points = [];

}

GameRoom.prototype.addEnemy = function(enemy)
{

}

GameRoom.prototype.removeEnemy = function(enemy_id)
{

}

GameRoom.prototype.addPlayer = function(player_id, player_name)
{

}

GameRoom.prototype.removePlayer = function(player_id, reason)
{

}

GameRoom.prototype.setPlayerAction = function(player_id, action)
{

}
GameRoom.prototype.broadcast = function(msg)
{
    var length = this.clients.length;
    for(var i =0; i < length; ++i)
    {
        this.clients[i].send(JSON.stringify(msg));
    }
}
try
{
    global.PING_PACKAGE = PING_PACKAGE;
    global.CONTROL_PACKAGE = CONTROL_PACKAGE;
    global.GAME_PACKAGE = GAME_PACKAGE
    global.GAME_CMD_PLAYER_JOIN = GAME_CMD_PLAYER_JOIN;
    global.GAME_CMD_PLAYER_SPAWN = GAME_CMD_PLAYER_SPAWN;
    global.GAME_CMD_PLAYER_DIE =GAME_CMD_PLAYER_DIE;
    global.GAME_CMD_PLAYER_FIRE = GAME_CMD_PLAYER_FIRE;
    global.GAME_CMD_PLAYER_OUT = GAME_CMD_PLAYER_OUT;
    global.GAME_CMD_PLAYER_STATE = GAME_CMD_PLAYER_STATE;
    global.GAME_CMD_PLAYER_CHAT = GAME_CMD_PLAYER_CHAT;

    global.CTRL_CMD_CLIENT_CONNECT = CTRL_CMD_CLIENT_CONNECT;
    global.CTRL_CMD_CLIENT_DISCONNECT = CTRL_CMD_CLIENT_DISCONNECT;
    global.CTRL_CMD_CLIENT_LOGIN = CTRL_CMD_CLIENT_LOGIN;
    global.CTRL_CMD_CLIENT_JOIN_ROOM = CTRL_CMD_CLIENT_JOIN_ROOM;
    global.CTRL_CMD_CLIENT_INFO = CTRL_CMD_CLIENT_INFO;
    global.CTRL_CMD_CREATE_ROOM = CTRL_CMD_CREATE_ROOM;
    global.CTRL_CMD_CLIENT_CHAT = CTRL_CMD_CLIENT_CHAT;
    global.CTRL_CMD_SERVER_INFO = CTRL_CMD_SERVER_INFO;
    global.CTRL_CMD_GAME_DATA = CTRL_CMD_GAME_DATA;
} catch(e)
{

}










